package com.example.demo;

import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestClient;
import java.util.List;

@Service
public class ProjectService {

    private final RestClient restClient;

    public ProjectService() {
        this.restClient = RestClient.builder()
                .baseUrl("")
                .defaultHeader("Accept", "application/json")
                .defaultHeader("User-Agent", "ProjectApiClient/1.0")
                .defaultHeader("Authorization", "Bearer user-ef7cb730-5459-4504-8b7f-cbb5e237b49a")
                .defaultHeader("x-coral-tenant", "139494637174848")
                .build();
    }

    @Tool(description = "获取项目总数")
    public int getProjectCount(
            @ToolParam(description = "项目类型") String type
    ) {
        // 发送请求获取项目列表
        ApiResponse<List<TreeDto>> response = restClient.get()
                .uri("/test/projects/org/list?type={type}", type)
                .retrieve()
                .body(new ParameterizedTypeReference<>() {});


        // 解析响应获取项目总数
        assert response != null;
        return response.getData().size();
    }

    @Tool(description = "获取项目列表")
    public List<TreeDto> getProjectList(
            @ToolParam(description = "项目类型") String type
    ) {
        // 发送请求获取项目列表
        ApiResponse<List<TreeDto>> response = restClient.get()
                .uri("/test/projects/org/list?type={type}", type)
                .retrieve()
                .body(new ParameterizedTypeReference<>() {});

        return response.getData();
    }
}